/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.list {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.ScrollableArray;
	import com.googlecode.milib.display.DuplicatedMCSNumberController;
	import com.googlecode.milib.display.IMCSCollectionController;
	import com.googlecode.milib.display.MCDuplicator;
	import com.googlecode.milib.display.MCSPositioning;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;
	import flash.display.Shape;

	/**
	 * @author Marek Brun
	 */
	public class MCList extends MLBroadcastClass implements IMLBroadcasterListener, IMCSCollectionController {

		public function get event_MCSSumChanged():String { return 'MCSSumChanged'; }
		
		private var model:ScrollableArray;
		private var duplicator:DuplicatedMCSNumberController;
		private var positioner:MCSPositioning;
		private var elementsDistance:Number;
		private var owner:MCListOwner;
		private var height:Number=100;
		private var width:Number=100;
		private var mlmc:MLMC;
		private var eleMC:MovieClip;
		private var mc_mask:MovieClip;
		private var btn:MLButton;
		private var elements:Array;
		
		public function MCList(mc:MovieClip, owner:MCListOwner) {
			mlmc=MLMC.forInstance(mc);
			eleMC=mlmc.getMC('elements.ele0');
			var mc_elements:MovieClip=mlmc.getMC('elements');
			var distantEleMC:MovieClip=mlmc.getMC('elements.ele1');
			mc_mask=new MovieClip();
			var rect:Shape=new Shape();
			rect.graphics.beginFill(0);
			rect.graphics.drawRect(0, 0, 100, 100);
			rect.graphics.endFill();
			mc_mask.addChild(rect);
			mc_mask.x=mlmc.getMC('elements').x;
			mc_mask.y=mlmc.getMC('elements').y;
			mlmc.mc.addChild(mc_mask);
			mlmc.getMC('elements').mask=mc_mask;
			
			btn=MLButton.forInstance(mc_elements);
			btn.disableButtonMode();
			btn.addListener(this);
			
			this.owner=owner;
			elements=[];
			elementsDistance=distantEleMC.y-eleMC.y;
			distantEleMC.parent.removeChild(distantEleMC);
			duplicator=new DuplicatedMCSNumberController(new MCDuplicator(eleMC));
			duplicator.addListener(this);
			positioner=new MCSPositioning(this, true);
			
			setModel(new ScrollableArray([]));
		}
		
		public function setHeight(height:Number):void {
			if(this.height==height){ return; }
			mc_mask.height=height;
			this.height=height;
			if(model) {
				model.setScrollScope(int(height/elementsDistance));
			}
		}
		
		public function getHeight():Number {
			return height;
		}
		
		public function setWidth(width:Number):void {
			if(this.width==width){ return; }
			mc_mask.width=width;
			this.width=width;
			var dataArr:Array=model.getCurrentArray();
			duplicator.setMCSNumber(dataArr.length);
			var i:uint;
			var loopElement:MCListAbstractElement;
			for(i=0;i<dataArr.length;i++){
				loopElement=elements[i];
				loopElement.setWidth(width);
			}
		}
		
		public function draw():void {
			var dataArr:Array=model.getCurrentArray();
			duplicator.setMCSNumber(dataArr.length);
			var i:uint;
			var loopElement:MCListAbstractElement;
			for(i=0;i<dataArr.length;i++){
				loopElement=elements[i];
				loopElement.setData(dataArr[i]);
			}
			broadcastEvent(event_MCSSumChanged);
		}
		
		public function setModel(model:ScrollableArray):void {
			if(this.model){
				this.model.removeListener(this);
			}
			this.model=model;
			this.model.addListener(this);
			model.setScrollScope(int(height/elementsDistance));
			draw();
		}
		
		public function getMCS():Array {
			return duplicator.getCurrentVisibleMCS();
		}

		public function getMCWidth(mc:MovieClip):Number {
			return NaN;
		}
		
		public function getMCHeight(mc:MovieClip):Number {
			return elementsDistance;
		}
		
//********************************************************************************************
//		events for MCList
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_ScrollChanged:
							draw();
						break;
					}
				break;
				case duplicator:
					switch(ev.event) {
						case duplicator.event_NewMC:
							elements[ev.data.index]=owner.createMCListElement(ev.data.mc, ev.data.index);
							elements[ev.data.index].setWidth(width);
						break;
					}
				break;
				case btn:
					switch(ev.event) {
						case btn.event_MouseWheelWhenOver:
							model.setScrollByOneUnit(ev.data.delta<0);
						break;
					}
				break;
			}
		}
	}
}
